package cn.wolfcode.shop.cloud.web.advice;

import cn.wolfcode.shop.cloud.CommonAdvice;
import cn.wolfcode.shop.cloud.Result;
import cn.wolfcode.shop.cloud.msg.MemberCodeMsg;

import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;

@ControllerAdvice
public class MemberControllerAdvice extends CommonAdvice {
    //处理validation.BindException参数校验异常
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Result handler(BindException ex){
        //获取到 参数校验出错的信息
        //String message = ex.getAllErrors().get(0).getDefaultMessage();
        System.out.println(ex.getAllErrors().get(0).getDefaultMessage());
        //MemberCodeMsg memberCodeMsg = MemberCodeMsg.ARGUMENTS_OPT;
        //memberCodeMsg.setMsg(MessageFormat.format(memberCodeMsg.getMsg(),message));
        MemberCodeMsg memberCodeMsg = MemberCodeMsg.ARGUMENTS_OPT.filterArg(ex.getAllErrors().get(0).getDefaultMessage());
        return Result.error(memberCodeMsg);

    }
}
